<template>
  <div class="contain">
    <el-card style="margin: 20px">
      <p v-if="$route.query.state">
        学科管理 <i class="el-icon-arrow-right" /> {{ $route.query.name }}
        <i class="el-icon-arrow-right" /> 标签管理
      </p>
      <el-divider v-if="$route.query.state" />
      <!-- 表单区域，收集信息 -->
      <el-form label-width="100px">
        <!-- 第一行标签 -->
        <el-row>
          <el-col :span="6">
            <!-- 可搜索 -->
            <el-form-item label="标签名称" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.tagName"
                size="small"
                placeholder="请输入标签名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <!-- 可搜索 -->
            <el-form-item label="状态" style="margin-bottom: 2px">
              <el-select v-model="searFrom.state" placeholder="请选择状态">
                <el-option label="启用" value="1" />
                <el-option label="禁用" value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="margin-bottom: 2px">
              <el-button size="small" @click="handelMationClean">
                清空
              </el-button>
              <el-button type="primary" size="small" @click="getTagList">
                搜索
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="6" style="text-align: right">
            <!-- 返回按钮 -->
            <el-button
              v-if="$route.query.state"
              type="text"
              @click="$router.push(`/disci/subject`)"
            >
              <span class="el-icon-back">返回学科</span>
            </el-button>
            <el-button
              type="success"
              class="right"
              size="small"
              @click="addOpen"
            >
              新增标签
            </el-button>
          </el-col>
        </el-row>
      </el-form>
      <!-- 提示文案 -->
      <el-alert type="info" :closable="false">
        <i class="el-icon-info" />共{{ total }}条记录</el-alert
      >
      <!-- 表单区域 -->
      <el-table :data="TagList">
        <el-table-column label="序号" prop="id" />
        <el-table-column label="所属学科" prop="subjectName" width="120px" />
        <el-table-column label="标签名称" prop="tagName" width="120px" />
        <!-- <el-table-column label="标签简称" prop="directoryName" width="120px" /> -->
        <el-table-column label="创建者" prop="username" width="220px" />
        <el-table-column label="创建日期" prop="addDate" width="220px" />
        <el-table-column label="面试题数量" prop="totals" width="120px" />

        <el-table-column label="状态" prop="state" width="120px">
          <template #default="{ row }">
            <!-- {{ row.state }} -->
            {{ row.state === 1 ? '已启用' : '禁用中' }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="230" fixed="right">
          <template #default="{ row }">
            <div>
              <!-- 启用按钮 -->
              <el-button type="text" @click="resetData(row)">
                {{ row.state === 1 ? '禁用' : '启用' }}
              </el-button>
              <!-- 修改按钮 -->
              <el-button
                type="text"
                :disabled="row.state === 1"
                @click="resetInterview(row)"
              >
                修改
              </el-button>
              <!-- 删除按钮 -->
              <el-button
                type="text"
                :disabled="row.state === 1"
                @click="handelDelInterview(row)"
              >
                删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block" style="text-align: right; margin-top: 10px">
        <el-pagination
          :current-page="searFrom.page"
          :page-sizes="[5, 8, 10, 15]"
          :page-size="searFrom.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 修改组件 -->
    <reset-info-com
      :show-dialog="resetDialog"
      :disic-data="subjectData"
      @closeDialog="resetClose"
    />
    <!-- 新增组件 -->
    <add-info-com :show-dialog="addDialog" @closeDialog="addClose" />
  </div>
</template>

<script>
import { getTagListAPI, delTagAPI, resetTagOPAPI } from '@/api/disic'
// 导入组件
import ResetInfoCom from './components/ResetInfoCom.vue'
import AddInfoCom from './components/AddInfoCom.vue'
export default {
  name: 'Laybel',
  components: { ResetInfoCom, AddInfoCom },
  data() {
    return {
      addDialog: false,
      resetDialog: false,
      subjectData: {},
      // 详情组件显示
      showDialog: false,
      // 搜索表单对象
      searFrom: {
        page: 1,
        pagesize: 10,
        // keyword: '',
        state: this.$route.query.state
      },
      total: 0,
      editForm: {},
      // 标签列表
      TagList: []
    }
  },
  created() {
    this.getTagList()
  },
  methods: {
    // 禁用状态
    async resetData(e) {
      if (e.state === 1) {
        await this.$confirm('确定启用该标签吗？', '温馨提示', {
          type: 'warning'
        })
        await resetTagOPAPI(e.id, 0)
        this.$message.success('启用成功')
        this.getTagList()
      } else {
        await this.$confirm('确定禁用该标签吗？', '温馨提示', {
          type: 'warning'
        })
        await resetTagOPAPI(e.id, 0)
        this.$message.success('禁用成功')
        this.getTagList()
      }
    },
    // 删除标签
    async handelDelInterview(quesData) {
      // console.log(quesData)
      await this.$confirm('该操作会永久删除该标签，确认删除吗？', '温馨提示', {
        type: 'warning'
      })
      // const resp =
      await delTagAPI(quesData.id)
      // console.log(resp)
      this.$message.success('删除成功')
      this.getTagList()
    },
    // 获取标签列表
    async getTagList() {
      const { data } = await getTagListAPI(this.searFrom)
      console.log('biaoqian列表', data)
      this.TagList = data.items
      this.total = data.counts
    },
    // 打开添加框
    addOpen() {
      this.addDialog = true
    },
    // 关闭添加框
    addClose() {
      this.addDialog = false
      this.getTagList()
    },
    // 关闭预览框
    resetClose() {
      this.resetDialog = false
      this.getTagList()
    },
    // 修改状态
    resetInterview(e) {
      // console.log(e)
      this.subjectData = e
      this.resetDialog = true
    },
    // 预览框打开时
    DialogShow(e) {
      // console.log(e)
      this.subjectData = e
      this.showDialog = true
    },
    // 关闭预览框
    closeDialog() {
      this.showDialog = false
    },
    // 当条数改变时
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.searFrom.pagesize = val
      this.getTagList()
    },
    // 当页数改变时
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.searFrom.page = val
      this.getTagList()
    },
    // 清空表单
    handelMationClean() {
      this.searFrom = {}
      this.getTagList()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-icon-arrow-right {
  color: #ccc;
}
.el-row {
  margin-bottom: 10px;
}
</style>

